<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Invoice extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	 public function insert_invoice(){
		
	 	$data = array(
	 		  ':p_invoice_num'  	=> $this->input->post('inv_num'), 
              ':p_bu_id'    		=> $this->session->userdata('bu_id'),
              ':p_customer_id'  	=> $this->input->post('acct_no'),
              ':p_account_num'  	=> $this->input->post('hidden_acct_num'),
              ':p_account_type'  	=> $this->input->post('hidden_type_code'),
              ':p_address_id'  		=> $this->input->post('hidden_address_id'),
              ':p_invoice_date'   	=> date('Y-m-d', strtotime($this->input->post('inv_date'))),
              ':p_gl_date'  		=> date('Y-m-d', strtotime($this->input->post('inv_glDate'))),
              ':p_charge_list_id'  	=> $this->input->post('hidden_chglist_id'),
              ':p_due_date'  		=> date('Y-m-d', strtotime($this->input->post('due_date'))),
              ':p_rec_acct_id'  	=> $this->input->post('rec_acct'),
              ':p_description'  	=> $this->input->post('inv_desc'),
              ':p_status'  			=> $this->input->post('inv_stat'),
              ':p_prev_bill_bal'    => $this->input->post('prev_bal'),
              ':p_user_id'			=> $this->session->userdata('user_id'),
              ':p_invoice_type' 	=> $this->input->post('inv_type')
              );

	 	$this->invoice_model->insert_invoice($data);
	 }

	 public function search_invoice(){
	 	$inv  = $this->input->post('s_invoice_no');
	 	$acct = $this->input->post('s_acct_no');
	 	$name = $this->input->post('s_cus_name');
	 	$inv_date = $this->input->post('s_inv_date');
	 	$query = $this->invoice_model->search_invoice($inv,$acct,$name,$inv_date);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
	 }

	 public function update_invoice(){
	 	$data = array(
	 		':p_rec_acct_id'		=> $this->input->post('edit_rec_acct'),
	 		':p_description'		=> $this->input->post('edit_inv_desc'),
	 		':p_status'				=> $this->input->post('edit_inv_stat'),
	 		':p_invoice_id'			=> $this->input->post('hidden_invEdit_id'),
	 		':p_user_id'			=> $this->session->userdata('user_id')
	 		);
	 	$this->invoice_model->update_invoice($data);
	 }	

	 public function insert_invoice_inline(){
		
	 	$data=array(
	 		  	':p_invoice_id'		=> $this->input->post('inline_invoice_id'),
				':p_line_num'		=> $this->input->post('inline_num'),
				':p_charge_code'	=> $this->input->post('inline_invchg'),
				':p_usage_qty'		=> $this->input->post('inline_quantity'),
				':p_uom_code'		=> $this->input->post('inline_uom'),
				':p_rate'			=> $this->input->post('inline_rate'),
				':p_tax_amt'		=> $this->input->post('inline_tax'),
				':p_line_amt'		=> $this->input->post('inline_amount'),
				':p_user_id'		=> $this->session->userdata('user_id')
              );

	 	$query = $this->invoice_model->insert_invoice_inline($data);
	 	if($query[0]->error == 1){
			echo json_encode($query);
		}else{
			$this->view_invoiceLine($this->input->post('inline_invoice_id'));
		}
	 }

	 public function view_invoiceLine($id){
		$query = $this->invoice_model->get_inv_line($id);
		echo json_encode($query);
	}

	 public function update_invoice_totalBAL(){
	 	$data = array(
	 		'invoice_total'		=> $this->input->post('amount'),
	 		'invoice_balance'	=> $this->input->post('amount')
	 		);
	 	$this->invoice_model->update_invoice_totalBAL($data,$this->input->post('invoice_id'));
	 }



	 public function get_account_data(){
	 	$query=$this->invoice_model->get_account_data($this->input->post('account'));
	 	$query2 = $this->invoice_model->get_list_options($query[0]->list_id);
	 	echo json_encode(array('result1' => $query , 'result2' => $query2));
	 }

	 public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
			'inv_status'	=> $this->invoice_model->get_stat(),
			'rec_acct'		=> $this->invoice_model->get_rec_acct_add(),
			'customers'		=> $this->invoice_model->get_cus_account(),	
			'invoice_num'	=> $this->invoice_model->get_invoice_next(),
			'uom'			=> $this->invoice_model->get_uom_data(),
			'invoice_t'	    => $this->invoice_model->get_types()
			));
	}
	public function edit_form($id){
		$qa = $this->invoice_model->get_invoice_payment_status($id);
		$val = '';
		foreach($qa as $q){
			if($q->line_amt == $q->pay_amt){
				$val = 'Fully Paid';
			}else if(empty($q->pay_amt)){
				$val = 'Unpaid';
			}else{
				$val = 'Partial Paid';
			}
		}

		$this->load->view($this->session->userdata('foredit'),array(
			'inv_status'	=> $this->invoice_model->get_stat(),
			'rec_acct'		=> $this->invoice_model->get_rec_acct(),
			'invoice_data'	=> $this->invoice_model->get_invoice_data($id),
			'invoice_pay'	=> $val
			));
	}
	public function view_form($id){
		$qa = $this->invoice_model->get_invoice_payment_status($id);
		$val = '';
		foreach($qa as $q){
			if($q->line_amt == $q->pay_amt){
				$val = 'Fully Paid';
			}else if(empty($q->pay_amt)){
				$val = 'Unpaid';
			}else{
				$val = 'Partial Paid';
			}
		}
		$this->load->view($this->session->userdata('forview'),array(
			'inv_status'	=> $this->invoice_model->get_stat(),
			'rec_acct'		=> $this->invoice_model->get_rec_acct(),
			'invoice_data'	=> $this->invoice_model->get_invoice_data($id),
			'invoice_pay'	=> $val
			));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */